package com.ltxtiyu.video.interceptor;

import com.ltxtiyu.video.common.JsonTool;
import com.ltxtiyu.video.common.RetEnum;
import com.ltxtiyu.video.common.SecurityConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;


public class CommonInterceptor implements HandlerInterceptor {

    private static Logger logger = LoggerFactory
            .getLogger(CommonInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {

        String method = request.getMethod();
        String channel = request.getParameter("channel");
        String signa = request.getParameter("signa");
        String timestamp = request.getParameter("ts");
        String appkey = "";

        // 校验请求方式
        if (!SecurityConstant.REQUEST_METHOD_POST.equals(method)) {
            logger.info("请求方式错误 method = " + method);
            writeRes(response, RetEnum.PARAM_ERROR);
            return false;
        }

        // 校验渠道
        if (SecurityConstant.CHANNEL_ANDROID.equals(channel)) {
            logger.info("android 请求,请求的url="+request.getRequestURL().toString());

            appkey = SecurityConstant.APPKEY_ANDROID;
        } else if (SecurityConstant.CHANNEL_IOS.equals(channel)) {
            logger.info("ios 请求,请求的url="+request.getRequestURL().toString());

            appkey = SecurityConstant.APPKEY_IOS;
        } else if (SecurityConstant.CHANNEL_M_SITE.equals(channel)) {
            logger.info("m站 请求,请求的url="+request.getRequestURL().toString());

            appkey = SecurityConstant.APPKEY_M_SITE;
        }else {
            logger.error("channel值错误 channel = " + channel);
            writeRes(response, RetEnum.PARAM_ERROR);
            return false;
        }

        //获取系统的时间戳
        /*long systemTimestamp = Calendar.getInstance().getTimeInMillis();
        long interval = (systemTimestamp - Long.valueOf(timestamp));
        if(interval>30000){
        	 logger.error("时间戳超时 timestamp = " + timestamp+";systemTimestamp = "+systemTimestamp+";超时间隔 interval = "+interval);
             writeRes(response, RetEnum.PARAM_ERROR);
             return false;
        }*/



        /******************************** 校验签名 *************************************/
        /*String s = SecurityConstant.APPKEY_MAP.get(appkey);
        String checkSigna = DigestUtils
                .md5Hex(appkey + DigestUtils.md5Hex(timestamp + SecurityConstant.APPKEY_MAP.get(appkey))).toUpperCase();
        if (!checkSigna.equals(signa)) {
            logger.error("签名校验失败 signa = " + signa);
            writeRes(response, RetEnum.PARAM_ERROR);
            return false;
        }*/
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        // TODO Auto-generated method stub

    }

    protected void writeRes(HttpServletResponse response, RetEnum re)
            throws Exception {
        String json = JsonTool.genJson(re);
        response.setHeader("Content-Type", "text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print(json);
        out.flush();
        out.close();
    }
}
